package org.rain.core.rbac.controller;

import org.rain.common.anno.Limit;
import org.rain.common.model.R;
import org.rain.common.util.bean.Beans;
import org.rain.common.util.Strs;
import org.rain.core.BaseController;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import org.rain.common.exception.BaseException;
import io.swagger.v3.oas.annotations.Operation;
import org.rain.core.util.Pages;
import org.springframework.web.bind.annotation.*;
import org.rain.core.querygen.QueryWrapperGenerate;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springdoc.core.annotations.ParameterObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import  org.rain.core.rbac.model.entity.SysRole;
import  org.rain.core.rbac.model.req.SysRoleReq;
import  org.rain.core.rbac.model.resp.SysRoleResp;
import  org.rain.core.rbac.service.ISysRoleService;

import java.util.Arrays;
import java.util.List;

/**
 * created by   on 2025年4月13日 16:01:41
 * <br/>
 * SysRole表控制器
 * <br/>
 */
@RestController
@RequestMapping("/sys/sysRole")
@Tag(name = "角色表")
public class SysRoleController extends BaseController<SysRole, ISysRoleService> {

    /**
     * 分页查询
     */
    @Limit
    @GetMapping("/page")
    @Operation(summary = "分页查询", description = "根据id查询对应的实体数据数据")
    public R<IPage<SysRoleResp>> page(@ParameterObject SysRoleReq sysRoledata,
                                       @RequestParam(defaultValue = "1") Long pageNo,
                                       @RequestParam(defaultValue = "10") Long pageSize) {
        QueryWrapper<SysRole> queryWrapper = QueryWrapperGenerate.init(sysRoledata);
        IPage<SysRole> page = new Page<>(pageNo, pageSize);
        baseService.page(page, queryWrapper);
        return R.success((Pages.transPage(page, SysRole::toResp)));
    }

    /**
     * 根据id查询数据
     */
    @GetMapping("/queryById")
    @Operation(summary = "根据id查询", description = "根据id查询数据")
    protected R<SysRoleResp> queryById(String id) {
        SysRole sysRole = baseService.getById(id);
        return R.success(sysRole.toResp());
    }

    /**
     * 新增或编辑数据
     */
    @PostMapping("/addOrUpdate")
    @Operation(summary = "新增或编辑", description = "根据有无主键id,新增或编辑数据")
    protected R<String> addOrUpdate(@RequestBody SysRoleReq sysRoledata) {
        SysRole sysRole = Beans.copyProperties(sysRoledata, SysRole.class);
        baseService.saveOrUpdate(sysRole);
        return R.success(sysRoledata.getId() == null ? "新增成功" : "编辑成功");
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    @Operation(summary = "删除", description = "根据id删除数据")
    protected R<String> delete(@Parameter(description = "主键id") String ids) {
        if (Strs.isEmpty(ids)) {
            throw new BaseException("删除接口的ids不能为空");
        }
        List<String> idList = Arrays.asList(ids.split(","));
        baseService.removeBatchByIds(idList);
        return R.success("删除成功");
    }

}